<template>
  <div>
    <div class="shopcart">
      <div class="content" @click="toggleList">
        <div class="content-left">
          <div class="logo-wrapper">
            <div class="logo" :class="{'highlight':totalCount>0}">
              <!--<i class="icon-shopping_cart" :class="{'highlight':totalCount>0}"></i>-->
            </div>
            <div class="num" v-show="totalCount>0">{{totalCount}}</div>
          </div>
          <div class="desc">
          	<p class="Highlight">{{payRepre}}</p>
          	<p>本店先消费后用餐</p>
          </div>
          <div class="price" v-show="totalPrice>0" :class="{'highlighter':totalPrice>0}"><span class="smallrmb">￥</span>{{totalPrice}}</div>
        </div>
        <div class="content-right" @click.stop.prevent="pay">
          <div class="pay" :class="payClass">
            {{payDesc}}
          </div>
        </div>
      </div>
      <div class="ball-container">
        <div v-for="ball in balls">
          <transition name="drop" @before-enter="beforeDrop" @enter="dropping" @after-enter="afterDrop">
            <div class="ball" v-show="ball.show">
              <div class="inner inner-hook"></div>
            </div>
          </transition>
        </div>
      </div>
      <transition name="fold">
        <div class="shopcart-list" v-show="listShow">
          <div class="list-header">
            <h1 class="title" style="text-align: center;width: 100%;">购物车</h1>
          </div>
          <div class="list-content" ref="listContent">
            <ul>
              <li class="minfoodList" v-for="food in selectFoods" v-show="food.count>0">
              	<div>
              		<p class="name">{{food.productName}}</p>
                  <p class="smallname">{{food.specNameList}}</p>
              	</div>
                <div class="price">
                  <span><span class="smallrmb">￥</span>{{food.productPrice*food.count}}</span>
                </div>
                <div class="cartcontrol-wrapper">
                  <cartcontrol @add="addFood" :food="food"></cartcontrol>
                </div>
              </li>
            </ul>
          </div>
        </div>
      </transition>
    </div>
    <transition name="fade">
      <div class="list-mask" @click="hideList" v-show="listShow"></div>
    </transition>
  </div>
</template>

<script type="text/ecmascript-6">
  import BScroll from 'better-scroll';
  import cartcontrol from 'components/cartcontrol/cartcontrol';
  import Bus from 'common/js/bus.js'
  export default {
    props: {
    	seller: {
        type: Object
      }
    },
    created() {
        Bus.$on('getTarget', target => {  
            this.addFood(target);
        });  
        Bus.$on('getselectFoods', selectFoods => {  
            this.selectFoods = selectFoods;
        });  
      },  
    data() {
      return {
        balls: [
          {
            show: false
          },
          {
            show: false
          },
          {
            show: false
          },
          {
            show: false
          },
          {
            show: false
          }
        ],
        dropBalls: [],
        fold: true,
        selectFoods:[]
      };
    },
    computed: {
      totalPrice() {
        let total = 0;
        this.selectFoods.forEach((food) => {
          total += food.productPrice * food.count;
        });
        return total;
      },
      totalCount() {
        let count = 0;
        this.selectFoods.forEach((food) => {
          count += food.count;
        });
        return count;
      },
      payRepre() {
        if (this.totalPrice === 0) {
          return '尚未下单，购物车是空的';
        } else if (this.totalPrice > 0) {
          return '消费金额';
        }
      },
      payDesc() {
        if (this.totalPrice === 0) {
          return '下单';
        } else if (this.totalPrice > 0) {
          return '去结算';
        }
      },
      payClass() {
        if (this.totalPrice === 0) {
          return 'not-enough';
        } else {
          return 'enough';
        }
      },
      listShow() {
        if (!this.totalCount) {
          this.fold = true;
          return false;
        }
        let show = !this.fold;
        if (show) {
          this.$nextTick(() => {
            if (!this.scroll) {
              this.scroll = new BScroll(this.$refs.listContent, {
                click: true
              });
            } else {
              this.scroll.refresh();
            }
          });
        }
        return show;
      }
    },
    methods: {
      drop(el) {
//      for (let i = 0; i < this.balls.length; i++) {
//        let ball = this.balls[i];
//        if (!ball.show) {
//          ball.show = true;
//          ball.el = el;
//          this.dropBalls.push(ball);
//          return;
//        }
//      }
      },
      toggleList() {
        if (!this.totalCount) {
          return;
        }
        this.fold = !this.fold;
      },
      hideList() {
        this.fold = true;
      },
      pay() {
        if (this.totalPrice === 0) {
          return;
        }else{
        	if(!sessionStorage.getItem("selectFoods")){
        		sessionStorage.setItem("selectFoods",JSON.stringify(this.selectFoods));
        	}else{
        		sessionStorage.removeItem("selectFoods");
        		sessionStorage.setItem("selectFoods",JSON.stringify(this.selectFoods));
        	}
					
					sessionStorage.setItem("totalPrice",JSON.stringify(this.totalPrice));
					sessionStorage.setItem("seller",JSON.stringify(this.seller));
        	this.$router.push({name:'order',params:{totalPrice:this.totalPrice}});
        }
      },
      addFood(target) {
        this.drop(target);
      },
      beforeDrop(el) {
        let count = this.balls.length;
        while (count--) {
          let ball = this.balls[count];
          if (ball.show) {
            let rect = ball.el.getBoundingClientRect();
            let x = rect.left - 32;
            let y = -(window.innerHeight - rect.top - 22);
            el.style.display = '';
            el.style.webkitTransform = `translate3d(0,${y}px,0)`;
            el.style.transform = `translate3d(0,${y}px,0)`;
            let inner = el.getElementsByClassName('inner-hook')[0];
            inner.style.webkitTransform = `translate3d(${x}px,0,0)`;
            inner.style.transform = `translate3d(${x}px,0,0)`;
          }
        }
      },
      dropping(el, done) {
        /* eslint-disable no-unused-vars */
        let rf = el.offsetHeight;
        this.$nextTick(() => {
          el.style.webkitTransform = 'translate3d(0,0,0)';
          el.style.transform = 'translate3d(0,0,0)';
          let inner = el.getElementsByClassName('inner-hook')[0];
          inner.style.webkitTransform = 'translate3d(0,0,0)';
          inner.style.transform = 'translate3d(0,0,0)';
          el.addEventListener('transitionend', done);
        });
      },
      afterDrop(el) {
        let ball = this.dropBalls.shift();
        if (ball) {
          ball.show = false;
          el.style.display = 'none';
        }
      }
    },
    beforeDestroy () {
	    Bus.$off('getselectFoods', selectFoods => {  
            this.selectFoods = selectFoods;
        });  
	  },
    components: {
      cartcontrol
    }
  };
</script>

<style lang="stylus" rel="stylesheet/stylus">
  @import "../../common/stylus/mixin.styl"
	.logo{
		background: url("http://115.29.189.195:9000/static/img/01home_shoppingcart.7284c4d.png") no-repeat left top;
		background-size: 46px 46px;
	}
	.highlight{
 		background: url("http://115.29.189.195:9000/tatic/img/01home_shoppingcart_s.ec162c7.png") no-repeat left top;
		background-size: 46px 46px
	}			
	.smallrmb{
		font-size: 9px;
	}		
	.shopcart .shopcart-list{
		border-radius: 8px;
		overflow: hidden;
	}
	.shopcart-main{
		width: 100%;
		height: 500px;
		background: #e8e8e8;
	}
	.shopcart-main .food{
		position: static;
	}
	.minfoodList{
		width: 90%;
		background: url(../../common/assets/01home_classify_s.png)no-repeat 12px center;
		background-size: 7px 7px;
		margin-left: 4%;
	}
	.minfoodList .name{
		overflow: hidden;
		text-overflow:ellipsis;
		white-space: nowrap;
		width: 110px;
	}
  .shopcart
    position: fixed
    left: 0
    bottom: 0
    z-index: 50
    width: 100%
    height: 48px
    .content
      display: flex
      background: rgba(0, 0, 0,.9)
      font-size: 0
      color: rgba(255, 255, 255, 0.4)
      .content-left
        flex: 1
        .logo-wrapper
          display: inline-block
          vertical-align: top
          position: relative
          top: -10px
          margin: 0 0 0 6px
          padding: 6px
          width: 60px
          height: 60px
          box-sizing: border-box
          border-radius: 100%
          .logo
            width: 100%
            height: 100%
            border-radius: 50%
            text-align: center
            .icon-shopping_cart
              line-height: 44px
              font-size: 24px
              color: #80858a
              &.highlight
                color: #fff
          .num
            position: absolute
            top: 2px
            right: 4px
            border:2px solid #fff
            width: 16px
            height: 16px
            line-height: 16px
            text-align: center
            border-radius: 16px
            font-size: 9px
            font-weight: 700
            color: #fff
            background: #e78a3d
            box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.4)
        .price
          display: inline-block
          vertical-align: top
          margin-top: 12px
          line-height: 24px
          float:right
          padding-right:6px
          box-sizing: border-box
          font-size: 16px
          &.highlighter
            color: #fff
        .desc
          display: inline-block
          vertical-align: top
          margin: 8px 0 0 4px
          line-height: 14px
          font-size: 10px
          .Highlight
          	font-size: 12px
          	line-height: 16px
          	color:#fff
      .content-right
        flex: 0 0 105px
        width: 105px
        .pay
          height: 48px
          line-height: 48px
          text-align: center
          font-size: 12px
          font-weight: 700
          &.not-enough
            background: #e78a3d
            color: rgba(255,255,255,.5)
          &.enough
            background: #e78a3d
            color: #fff
    .ball-container
      .ball
        position: fixed
        left: 32px
        bottom: 22px
        z-index: 200
        transition: all 0.4s cubic-bezier(0.49, -0.29, 0.75, 0.41)
        .inner
          width: 16px
          height: 16px
          border-radius: 50%
          background: #e78a3d
          transition: all 0.4s linear
    .shopcart-list
      position: absolute
      left: 5%
      top:-8px
      z-index: -1
      width: 90%
      transform: translate3d(0, -100%, 0)
      &.fold-enter-active, &.fold-leave-active
        transition: all 0.5s
      &.fold-enter, &.fold-leave-active
        transform: translate3d(0, 0, 0)
      .list-header
        height: 40px
        line-height: 40px
        padding: 0 18px
        background: #f3f5f7
        border-bottom: 1px solid rgba(7, 17, 27, 0.1)
        .title
          float: left
          font-size: 14px
          color: rgb(7, 17, 27)
        .empty
          float: right
          font-size: 12px
          color: rgb(0, 160, 220)

      .list-content
        max-height: 217px
        overflow: hidden
        background: #fff
        .minfoodList
          position: relative
          padding: 12px 0 12px 10%
          box-sizing: border-box
          border-1px(rgba(7, 17, 27, 0.1))
          .name
            line-height:24px
            font-size: 14px
            color: rgb(7, 17, 27)
          .smallname
          	font-size: 12px
          	color:#93999f
          	padding-left:2px
          .price
            position: absolute
            right: 90px
            top: 12px
            line-height: 24px
            font-size: 14px
            font-weight: 700
            color: #323232
          .cartcontrol-wrapper
            position: absolute
            right: 0
            top: 11px

  .list-mask
    position: fixed
    top: 0
    left: 0
    width: 100%
    height: 100%
    z-index: 20
    backdrop-filter: blur(10px)
    opacity: 1
    background: rgba(7, 17, 27, 0.6)
    &.fade-enter-active, &.fade-leave-active
      transition: all 0.5s
    &.fade-enter, &.fade-leave-active
      opacity: 0
      background: rgba(7, 17, 27, 0)
</style>